<template>
  <div>
     <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col  :span="8">
          <!-- 双向绑定我的页面数据，有什么他会返给我什么点击重新获取页面 -->
       <el-input placeholder="请输入内容" @clear="goodlist" class="input-with-select" v-model="queryData.query">
        <el-button slot="append" icon="el-icon-search" @click="goodlist"></el-button>
      </el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="$router.push('/goods/add')"  type="primary">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goods">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column width="100" prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column width="100" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column width="150" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.upd_time | formatDate}}
          </template>
        </el-table-column>
        <el-table-column width="150" label="操作">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini"></el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="removeGoods(scope.row.goods_id)"></el-button>
          </template>
      </el-table-column>
      </el-table>
      <!-- 分页 -->
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- handleSizeChange里的形参 val接收的是我 -->
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取后台返回来的商品分类数据
      goods: [],
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0

    }
  },

  created () {
    this.goodlist()
  },
  // 接收后台传来的数据
  methods: {
    async goodlist () {
      const { data: res } = await this.$axios.get('goods', {
        params: this.queryData
      })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      // 接收后台传过来的商品分类数据
      this.goods = res.data.goods
      // 接受后台传过来多少条数据
      this.total = res.data.total
    },
    // 接受最新页面选中该每页多少条数据
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.goodlist()
      // 重新获取页面
    },
    // 页码改变页面
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.goodlist()
    },
    removeGoods (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete('goods/' + id)
        if (res.meta.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除成功!')
        this.goodlist()
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  }
}
</script>

<style scoped lang='less'>

</style>
